// pages/coupon_package/coupon_send/coupon_send.js
import {
    wxRequest
} from '../../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgUrl: app.globalData.imgUrl,

        agentItem: {},
        storeItem: {},

        isSup: false,
        isAgent: false,

        orgType: ''
    },

    //选择代理商
    handleAgent: function (e) {
        console.log(e);
        wx.navigateTo({
            url: '/pages/publish/template/agent',
        })
    },

    // 请选择店铺
    handleStore: function () {
        wx.navigateTo({
            url: '/pages/publish/template/store',
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.data.orgType = wx.getStorageSync('userInfo').orgType
        if (this.data.orgType === 4) {
            this.data.isSup = true
            this.data.isAgent = false
        } else if (this.data.orgType === 2) {
            this.data.isAgent = true
            this.data.isSup = false
        }
        this.setData({
            isAgent: this.data.isAgent,
            isSup: this.data.isSup,
            orgType: this.data.orgType
        })
    },

    // 提交生成优惠券
    formSubmit: function (e) {
        var formdata = e.detail.value;
        let url = ''
        let params = {}

        if (params.sentQty === '') {
            wx.showToast({
                title: '请填入数量!',
                icon: 'error'
            })
            return;
        }

        

        if (this.data.orgType === 4) {
            if (!this.data.agentItem.id) {
                wx.showToast({
                    title: '请选择代理商!',
                    icon: 'error'
                })
                return;
            }
            url = '/odr/protected/voucher/sendToAgent'
            params = {
                sentQty: parseInt(formdata.sentQty),
                ageId: this.data.agentItem.id,
                vouId: wx.getStorageSync('vouId')
            }
        } else {
            if (!this.data.storeItem.id) {
                wx.showToast({
                    title: '请选择店铺!',
                    icon: 'error'
                })
                return;
            }
            url = '/odr/protected/voucher/sendToStore'
            params = {
                sentQty: parseInt(formdata.sentQty),
                stoId: this.data.storeItem.id,
                vouId: wx.getStorageSync('vouId'),
                userId: wx.getStorageSync('userInfo').id
            }
        }

        console.log(typeof params.sentQty);

        // 请求
        _wxRequest.wxTonkenAjax({
            url: app.globalData.apiUrl + url,
            method: 'POST',
            data: params,
        }, (res) => {
            wx.showToast({
                title: '赠送成功!',
                icon: 'success'
            })
            wx.setStorageSync('isSent', true)
            setTimeout(() => {
                wx.redirectTo({
                    url: '/pages/coupon_package/coupon_index',
                })
            }, 1500)
        })

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        wx.removeStorageSync('vouId')
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})